{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "af633de8",
   "metadata": {},
   "source": [
    "# 本周内容：\n",
    "一、函数的定义\n",
    "1、写def：def定义函数第一步\n",
    "2、写定义函数的名称： def function_name\n",
    "-1、function_name遵循变量名称定义的规则\n",
    "3、函数名称后加上“（）”：def function_name（）：====》 f(X)= 2x\n",
    "# 书写函数体\n",
    "1、函数的注释\n",
    "2、写函数的具体操作（函数体）\n",
    "3、注意：函数也是需要有tab键的间距\n",
    "# 使用函数（函数的调用）\n",
    "1、function_name（）\n",
    "---\n",
    "def：定义函数的关键字 \n",
    "fib：函数名\n",
    "(n):定义函数的参数\n",
    "  代码块：\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a, end=' ')\n",
    "        a, b =b, a+b\n",
    "    print()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ba4e1b41",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a, end=' ')\n",
    "        a, b =b, a+b\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "de85c505",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 "
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3c2a4a9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():\n",
    "    #打印hello world\n",
    "    print(\"hello world\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "03f07323",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0d3d6d80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:hello\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 函数存在的意义\n",
    "vowels = set('aeiou')\n",
    "word = input(\"provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f209c867",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"打印文本中出现的元音字母\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))  #交集运算\n",
    "    print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2c3253e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word)\n",
      "    打印文本中出现的元音字母\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "aa3c8cfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"djhge\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a72ba28",
   "metadata": {},
   "source": [
    "函数存在的意思\n",
    "1、可以同过调用函数的方式实现某些功能，使用人不需要考虑函数代码内容\n",
    "2、节省时间、提升应用的效率\n",
    "3、函数一旦定义，可重复使用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f0891b2",
   "metadata": {},
   "source": [
    "3、函数的参数\n",
    "1\\形式参数  在定义过程中，函数的参数为形式参数\n",
    "2、实际参数    调用函数过程中，函数的参数为实际参数。当调用函数时，形式参数被分配空间，成为实际参数的应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "be633c24",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    '''显示简单的问候语 ，输入你的名字'''\n",
    "    print(\"hello,\"+ username.title()+\"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "78520be5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,Jinshan!\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"jinshan\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "41dd3a51",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a\" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is\" + pet_name.title()+ \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1ece71ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have adog.\n",
      "Mydog's name isDoudou.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\",\"doudou\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "802ed4b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "当参数不止有这一个时\n",
    "1、位置参数   形式参数有多个值，如果实际参数直接赋值，要按照形式参数的先后顺序。这种方式是 位置参数赋值。1、考虑位置\n",
    "                                                                                          2、考虑个数\n",
    "2、关键字参数\n",
    "3、默认参数 在默认参数中，写在形式参数中\n",
    "默认参数存在的话，实际调用过程中可以省略赋值的过程"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
